package com.feng.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.feng.common.utils.Asserts;
import com.feng.common.utils.SecurityUtil;
import com.feng.mbg.domain.SysPermission;
import com.feng.mbg.domain.SysRole;
import com.feng.mbg.domain.SysRoleExample;
import com.feng.mbg.mapper.SysRoleMapper;
import com.feng.system.dao.SysPermissionDao;
import com.feng.system.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Feng
 * @since 2022/4/20 16:23
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    SysPermissionDao permissionDao;

    @Autowired
    SysRoleMapper roleMapper;

    @Override
    public List<SysPermission> getPermissionsByRoleId(Long roleId) {
        return permissionDao.selectByRoleId(roleId);
    }

    @Override
    public PageInfo<SysRole> search(String keyWord, Integer pageNum, Integer pageSize) {
        SysRoleExample example = new SysRoleExample();
        example.createCriteria()
                .andRoleNameLike("%" + keyWord + "%")
                .andIsDeletedEqualTo(0);
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(roleMapper.selectByExample(example));
    }

    @Override
    public void addRole(String roleName, Integer level) {
        if (SecurityUtil.getCurrentRoleLevel() > level) {
            Asserts.fail("不能创建等级大于自己的角色");
        }
        if (getRoleByRoleName(roleName) != null) {
            Asserts.fail("角色名为" + roleName + "的角色已存在");
        }
        SysRole role = new SysRole();
        role.setRoleName(roleName);
        role.setLevel(level);
        role.setCreateTime(new Date());
        roleMapper.insertSelective(role);
    }

    @Override
    public void updateRoleById(Long roleId, String roleName, Integer level) {

        SysRole role = roleMapper.selectByPrimaryKey(roleId);
        if (role == null || role.getIsDeleted().equals(1)) {
            Asserts.fail("修改的角色不存在");
        }
        int currentLevel = SecurityUtil.getCurrentRoleLevel();
        if (currentLevel > role.getLevel()) {
            Asserts.fail("不能修改等级高于自己的角色");
        }
        if (currentLevel > level) {
            Asserts.fail("修改后角色等级不能高于修改者");
        }
        if (getRoleByRoleName(roleName) != null) {
            Asserts.fail("角色名为" + roleName + "的角色已存在");
        }
        role.setRoleName(roleName);
        role.setLevel(level);
        role.setUpdateTime(new Date());
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void deleteByRoleIds(Set<Long> roleIds) {
        if (CollectionUtil.isEmpty(roleIds)) {
            return;
        }
        int currentLevel = SecurityUtil.getCurrentRoleLevel();
        for (Long roleId : roleIds) {
            SysRole role = roleMapper.selectByPrimaryKey(roleId);
            if (role == null || role.getIsDeleted().equals(1)) {
                continue;
            }
            if (currentLevel > role.getLevel()) {
                Asserts.fail("不能删除等级高于自己的角色");
            }
        }
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRoleIdIn(new ArrayList<>(roleIds));
        SysRole role = new SysRole();
        role.setIsDeleted(1);
        roleMapper.updateByExampleSelective(role, example);
    }

    private SysRole getRoleByRoleName(String roleName) {
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRoleNameEqualTo(roleName);
        List<SysRole> roles = roleMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(roles)) {
            return null;
        }
        return roles.get(0);
    }
}
